package com.helloandroid.models

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.helloandroid.*
import com.helloandroid.app.model.AppCache
import com.helloandroid.ext.parseBoardInfo
import com.helloandroid.ext.parseHongBaoYu
import com.helloandroid.ext.process
import com.helloandroid.tools.Http.asyncHttpReq
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.NetWorkUtil
import com.helloandroid.vo.BoardInfo
import com.helloandroid.vo.HbyInfo
import kotlinx.coroutines.*
import org.json.JSONObject


/**
 * firstUsed: 1 表示已经领取  0表示还未领取
 *
 */
data class BsData(var firstUsed: Int,var bsCount: Int)
class ZouViewModel : ViewModel() {

    var tickRunning = false

    fun getInfo(callback:(Boolean,BoardInfo?)->Unit) {

        val info = AppCache.copyBoardInfo()
        if (info != null){
            callback(true,info)
            return
        }

        if (!NetWorkUtil.networkConnected()){
            AppUtil.toast("请打开网络.")
            callback(false,null)
            return
        }

        viewModelScope.launch {
            val url = "dashboard/info?user_id=${AppUtil.user.user_id}"
            val jsonObj = JSONObject(asyncHttpReq(url))
            val parseResult = jsonObj.process(JSONObject::parseBoardInfo)

            if (parseResult.err == 0) {
                val boardInfo = parseResult.obj as BoardInfo
                AppCache.boardInfo = boardInfo
                callback(true,AppCache.copyBoardInfo())
            } else {
                MyLog.elog("err:${parseResult.err},msg:${parseResult.msg}")
                callback(false,null)
            }
        }
    }

    fun getLastHbyTime(callback:(Boolean, HbyInfo?)->Unit){

        val info = AppCache.copyHbyInfo()
        if (info != null){
            callback(true,info)
            return
        }

        if (!NetWorkUtil.networkConnected()){
            callback(false,null)
            return
        }

        viewModelScope.launch {
            val url = "game/hongbaoyu/info?user_id=${AppUtil.user.user_id}"
            val jsonObj = JSONObject(asyncHttpReq(url))
            val parseResult = jsonObj.process(JSONObject::parseHongBaoYu)
            if (parseResult.err == 0){
                val hbyInfo = parseResult.obj as HbyInfo
                AppCache.hbyInfo = hbyInfo
                callback(true,AppCache.copyHbyInfo())
            }else{
                MyLog.elog("err:${parseResult.err},msg:${parseResult.msg}")
                callback(false,null)
            }
        }
    }

    fun tick(tickUpdate:()->Unit){
        if (tickRunning){
            return
        }
        tickRunning = true
        viewModelScope.launch {
            launch(Dispatchers.Default){
                while (isActive && tickRunning){
                    delay(1000L)
                    withContext(Dispatchers.Main){
                        tickUpdate()
                    }
                }
            }
        }
    }

    fun stopTick(){
        tickRunning = false
    }

    fun startPerSecondTick(action:()->Unit){
        viewModelScope.launch {
            launch(Dispatchers.Default){
                while (isActive){
                    delay(1000)
                    withContext(Dispatchers.Main){
                        action()
                    }
                }
            }
        }
    }

    override fun onCleared() {
        tickRunning = false
        super.onCleared()
    }
}